package com.example.tao.smartcar.wifi;

import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.tao.smartcar.BaseActivity;
import com.example.tao.smartcar.R;
import com.example.tao.smartcar.wifi.LAN.WifiTool;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

public class WifiConnectActivity extends BaseActivity implements View.OnClickListener {

    public static final int PORT = 10003;

    private EditText ipEditText, portEditText;
    private Button sendBtn;
    private Button scanBtn;
    private Button setBtn;
    private String ip;
    private int port;
    private WifiTool mWifiTool;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi_connect);

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());

        mWifiTool = new WifiTool(this.getApplication());

        initView();
    }

    private void initView() {
        ipEditText = (EditText) this.findViewById(R.id.ip);
        portEditText = (EditText) this.findViewById(R.id.port);
        sendBtn = (Button) this.findViewById(R.id.btn_send);
        scanBtn = (Button) this.findViewById(R.id.btn_scan);
        setBtn = (Button) this.findViewById(R.id.btn_set);

        sendBtn.setOnClickListener(this);
        scanBtn.setOnClickListener(this);
        setBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_send :
                ip = "10.0.255.231";
                port = PORT;
                new Thread(){
                    @Override
                    public void run() {
                        try{
                            Socket socket = new Socket(ip, port);
                            OutputStream outputStream = socket.getOutputStream();
                            outputStream.write(1000);
                            outputStream.close();
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
            case R.id.btn_scan:
                mWifiTool.openWifi();
                mWifiTool.scanWifi();

                mWifiTool.scanOtherSTAs();
                List<String> ipList = mWifiTool.getIpList();

                for (int i = 0; i < ipList.size(); i++) {
                    ip = ipList.get(i);


                }
                break;
            case R.id.btn_set :
                ip = mWifiTool.getAPAddress();
                port = 333;
                new Thread(){
                    @Override
                    public void run() {
                        Socket socket = null;
                        try{
                            socket = new Socket(ip, port);
                            OutputStream outputStream = socket.getOutputStream();
                            outputStream.write("guardian-2.4g".getBytes());
                            /*
                            outputStream.write('u');
                            outputStream.write('a');
                            outputStream.write('r');
                            outputStream.write('d');
                            outputStream.write('i');
                            outputStream.write('a');
                            outputStream.write('n');
                            */
                            outputStream.close();

                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            if (socket != null) {
                                try {
                                    socket.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }.start();
                break;
        }

    }
}
